<?php
declare(strict_types=1);
/**
 * 专题馆组件
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/5/16
 * Time: 18:18:56
 */

namespace Service\LayoutService;

use Service\ActivityService\ActivityStoreService;
use App\Model\BbcModel\Specialtopic;

class subject extends \Service\BaseService
{
    /**
     * @param $mnid
     * @param $cid
     * @param $mn_sid
     * @param $mid
     * @param $moduleData
     * @return array|\think\response\Json
     */
    public function getDataList($mnid,$cid,$mn_user,$mid,$moduleData,$extra){
        $mn_sid = $mn_user['id'];
        $sub_entry = $moduleData['setStyle']['sub_entry'] ?? [];
        if(count($sub_entry) <= 0){
            return [];
        }
        $storeActivity = $extra['storeActivity'] ?? [];
        //获取店铺活动数据
        $activityID = $storeActivity['seckillActivity']['id'] ?? 0;
        $mn_secskill_activity = ActivityStoreService::instance()->getMnActivityOne((int)$cid,(int)$mn_sid,$activityID);

        $special_ids = array_column($sub_entry,'id');

        $special_list = Specialtopic::instance()
            ->where([
                ['cid','=',$cid],
                ['df','=',0],
                ['status','=',1]
            ])
            ->whereIn('id',$special_ids)
            ->select(['id','pic','name'])
            ->get()->toArray();
        if(count($special_list) <= 0){
            return [];
        }

        $special_list_ids = array_column($special_list,'id');
        $special_list_map = array_column($special_list,null,'id');
        foreach ($sub_entry as $k=>$v){
            if(!in_array($v['id'],$special_list_ids)){ //专题馆不正常
                unset($sub_entry[$k]);
                continue;
            }
            $sub_entry[$k]['title'] = $special_list_map[$v['id']]['name'] ?? '';
            $sub_entry[$k]['image_url'] = $special_list_map[$v['id']]['pic'] ?? '';
        }
        if(!$sub_entry){
            return [];
        }
        $sub_entry = array_values($sub_entry);
        unset($moduleData['setStyle']['sub_entry']);
        $moduleData['setStyle']['sub_entry'] = $sub_entry;
        return $moduleData;

    }
}